package com.team.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * @author keluoyisi
 * @program: firstitem
 * @description:
 * @date 2022-10-12 15:12:30
 */
@WebFilter(urlPatterns = "/*")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        String path = request.getRequestURI();
        if(path.equals("/login.html")||path.equals("/login/admin") || path.equals("/index.jsp") || path.equals("/")
                ||path.startsWith("/static/") || path.startsWith("/indexTitle/")
                || path.startsWith("/SectionAndDoctor/") || path.startsWith("/ReservationQian/") || path.startsWith("/download")
                || path.startsWith("/SectionAndAdmin/")|| path.startsWith("/css/")
                ||path.endsWith(".css") ||path.endsWith(".js")||path.endsWith(".jpg")||path.endsWith(".png")||path.endsWith(".jpeg")
                ||path.startsWith("/appointment/")
                ||path.startsWith("/AppointmentController/")
                ||path.startsWith("/ManaginePrivilegesController/")
                ||path.startsWith("/AliPay/")||path.startsWith("/captcha")

        ){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        if(session.getAttribute("admin")!=null ){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        response.sendRedirect("/login.html");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
}

